﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Zeus.DataTrans.XML.Analyzer
{
    public class XmlManager
    {
        private XmlDocument xmlDoc;
        private XmlNamespaceManager xnm;
        private string prefix;
        private string nameSpace;

        public XmlManager(string xml, bool fileMode) : this(xml, fileMode, "", "")
        {
            
        }

        public XmlManager(string xml, bool fileMode, string prefix, string nameSpace)
        {
            if (fileMode)
                LoadXmlByPath(xml);
            else
                LoadXmlByContent(xml);
            
            this.prefix = prefix;
            this.nameSpace = nameSpace;
            xnm = new XmlNamespaceManager(xmlDoc.NameTable);
            xnm.AddNamespace(prefix, nameSpace);
        }

        public void LoadXmlByPath(string xmlPath)
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
        }

        public void LoadXmlByContent(string xmlContent)
        {
            string abc = xmlContent;
            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlContent);
        }

        public XmlNode GetRootNodeNm(string nodeName)
        {
            XmlNode oRootNode = xmlDoc.SelectSingleNode("//" + prefix + ":" + nodeName, xnm);

            return oRootNode;
        }

        public XmlNode GetSingleNodeNm(XmlNode parentNode, string nodeName)
        {
            if (parentNode != null)
            {
                XmlNode node = parentNode.SelectSingleNode(prefix + ":" + nodeName, xnm);
                return node;
            }

            return null;
        }

        public XmlNodeList GetNodesNm(XmlNode parentNode, string nodeName)
        {
            if (parentNode != null)
            {
                XmlNodeList nodes = parentNode.SelectNodes(prefix + ":" + nodeName, xnm);

                return nodes;
            }

            return null;
        }
    }
}
